package com.jkl.controller.param;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jkl.entity.BaseResponseDTO;
import com.jkl.entity.param.BSParam;
import com.jkl.service.param.inter.IBSParamService;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * <p>
 * 123开放层接口
 * <p>
 * <p>
 * 变更说明：wangjingxiang 2017年12月28日 123 增加新增、更新、删除方法
 * <p>
 * Date:2018年10月03日
 * 
 * @author ji.k
 * @version 1.0
 */
@RestController
@RequestMapping("/jky/param")
@Slf4j
public class BSParamController {

	@Autowired
	private HttpServletRequest request;
	@Autowired
	private IBSParamService paramService;

	@PostMapping("/queryParamByGroupCode")
	public BaseResponseDTO queryParamByGroupCode(@RequestBody BSParam param) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		List<BSParam> paramList = paramService.queryParamByGroupCode(param);
		JSONObject returnJson = new JSONObject();
		JSONArray paramArray = (JSONArray) JSONArray.toJSON(paramList);
		returnJson.put("paramList", paramArray);
		returnData.setReturnData(returnJson);
		return returnData;
	}

	@PostMapping("/queryParamGroupList")
	public BaseResponseDTO queryParamGroupList() {
		BaseResponseDTO returnData = new BaseResponseDTO();
		List<BSParam> paramList = paramService.queryParamGroupList();
		JSONObject returnJson = new JSONObject();
		JSONArray paramArray = (JSONArray) JSONArray.toJSON(paramList);
		returnJson.put("paramList", paramArray);
		returnData.setReturnData(returnJson);
		return returnData;
	}

	@PostMapping("/queryParamByEntity")
	public BaseResponseDTO queryParamByEntity(@RequestBody BSParam param) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		List<BSParam> paramList = paramService.queryParamByEntity(param);
		JSONObject returnJson = new JSONObject();
		JSONArray paramArray = (JSONArray) JSONArray.toJSON(paramList);
		returnJson.put("paramList", paramArray);
		returnData.setReturnData(returnJson);
		return returnData;
	}

	@PostMapping("/queryNextCodeByGroup")
	public BaseResponseDTO queryNextCodeByGroup(@RequestBody BSParam param) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		BSParam bSParam = paramService.queryNextCodeByGroup(param);
		JSONObject returnJson = (JSONObject) JSON.toJSON(bSParam);
		returnData.setReturnData(returnJson);
		return returnData;
	}

	@PostMapping("/deleteParam")
	public BaseResponseDTO deleteParam(@RequestBody BSParam param) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		try {
			boolean result = paramService.deleteParam(param);
			if (!result) {
				returnData.setResultCode("300001").setResultMessage("参数删除失败");
			}
		} catch (Exception e) {
			returnData.setResultCode("300002").setResultMessage("参数删除失败");
		}
		return returnData;
	}

	@PostMapping("/addParam")
	public BaseResponseDTO addParam(@RequestBody BSParam param) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		try {
			int result = paramService.addParam(param);
			if (result < 1) {
				returnData.setResultCode("400001").setResultMessage("新增参数失败");
			}
		} catch (Exception e) {
			returnData.setResultCode("400002").setResultMessage("新增参数失败");
		}

		return returnData;
	}
	
	@PostMapping("/queryInit") // 加载初始化
	public BaseResponseDTO queryInit(@RequestParam(value = "groupCode") String groupCode) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		PageInfo<BSParam> projectList = paramService.queryInit(groupCode);
		JSONObject returnJson = new JSONObject();
		returnJson.put("projectList", projectList);
		returnData.setReturnData(returnJson);
		return returnData;
	}
}